import scrapy
from ..items import DoubanItem

class DoubanSpider(scrapy.Spider):
    name = "douban"
    allowed_domains = ["movie.douban.com"]
    start_urls = ["https://movie.douban.com/top250"]

    def parse(self, response):
        doubans = response.xpath("//ol[@class='grid_view']/li")
        for douban in doubans:
            item = DoubanItem()
            item['name'] = douban.xpath("div[@class='item']/div[2]/div[1]/a/span/text()").extract_first()
            item['imgUrl'] = douban.xpath("div/div[@class='pic']/a/@href").extract_first()
            text = douban.xpath("div/div[@class='info']/div[@class='bd']/p/text()").extract()[1]

            # fs_text = douban.xpath("div/div[@class='info']/div[@class='bd']/p/text()").extract()[0]
            # item['author'] = fs_text.split(" ")[0].strip().split(" ")[1: -1]
            # item['score'] = douban.xpath("div/div[@class='info']/div[2]/div[@class='star']/span[2]/text()").extract_first()
            item['author'] = douban.xpath('div[@class = "bd"]/p/text()').extract_first()
            item['score'] = douban.xpath('/div/div[2]/div[2]/div/span[2]')

            c_start = text.find("/")
            c_end = text.find("/", c_start + 1)
            country = text[c_start + 1: c_end]
            year = text[: c_start]
            types = text[c_end:]

            country_analyse = country.split(" ")
            country_have = country_analyse[1].split(" ") if len(country_analyse) > 1 else country_analyse[0].split(" ")
            country = country_have if country_have != [""] else ["中国大陆"]
            item['country'] = country[0]
            item['year'] = year.split(" ")[0].strip()
            item['types'] = types.split(" ")[1].strip().split(" ")

            yield item

            next_page = response.xpath("/html/body/div[3]/div[1]/div[1]/div[1]/div[2]/span[3]/a/@href").extract_first()
            if next_page:
                yield response.follow(next_page, self.parse)

